﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Core;
using System.Drawing;
using Silvermoon.Visuals;
using Silvermoon.Images;
using Silvermoon.Utils;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Provides a modal dialog.
    /// </summary>
    public class Dialog : DialogBase
    {

        private DialogResultEventHandler onClose;
        private ModalBackground modalBackground;
        private BackgroundVisual background;
        private Color color;
        private int modalAlpha = 190;

        private bool isModal;

        public Dialog()
            : base()
        {
            background = new BackgroundVisual { Texture = SysImages.MenuBackground, Beveled = false };
            ModalColor = Color.Black;
            //         Clipping = new Margin(8);
            color = SysColors.DialogColor;
            TextColor = SysColors.DialogTextColor;
            Visuals = new Visual[] { background };
        }


        public override void Dispose()
        {
            if (background != null) background.Dispose();
            if (modalBackground != null) modalBackground.Dispose();
            Visuals = null;

            base.Dispose();
        }

        /// <summary>
        /// Get or sets the alpha value for the background when the dialog is shown modal.
        /// </summary>
        public int ModalAlpha
        {
            get { return modalAlpha; }
            set
            {
                modalAlpha = ValidateAlpha(value);
            }
        }

        /// <summary>
        /// Gets or sets the color for the background when this dialog is shown modal.
        /// </summary>
        public Color ModalColor { get; set; }

        public Color Color
        {
            get { return color; }
            set
            {
                if (color != value)
                {
                    color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        public override void Show(TransitionMode mode)
        {
            isModal = false;
            base.Show(mode);
        }

        public virtual void ShowModal(TransitionMode mode)
        {
            isModal = true;
            if (modalBackground == null)
            {
                modalBackground = new ModalBackground { Alpha = modalAlpha };
            }
            modalBackground.Color = ModalColor;
            Screen.Current.Attach(modalBackground);
            base.Show(mode);
        }

        public virtual void ShowModal(TransitionMode mode, DialogResultEventHandler onClose)
        {
            this.onClose = onClose;
            ShowModal(mode);
        }

        protected override void OnHidden()
        {
            if (modalBackground != null)
            {
                Screen.Current.Detach(modalBackground);
                modalBackground.Dispose();
                modalBackground = null;
            }
            base.OnHidden();
            OnClosed(Result);
        }

        protected virtual void OnClosed(DialogResult result)
        {
            if (onClose != null) onClose(this, new DialogEventArgs { Result = result });
            if (Closed != null) Closed(this, new DialogEventArgs { Result = result });
        }

        protected override void OnTransitionProgress(object sender, AnimEventArgs e)
        {
            if (isModal && modalBackground != null)
            {
                int value = ((CurrentTransition & TransitionMode.Hide) != 0) ? e.EndValue - e.Value : e.Value;
                modalBackground.Alpha = (modalAlpha * value / e.EndValue);
            }
            base.OnTransitionProgress(sender, e);
        }

        protected override void OnLayout()
        {
            background.Size = size;
            if (CheckFlag(InvalidationFlag.Color))
            {
                background.Colors = ColorUtil.CreatePlasticColors(color);
            }
            base.OnLayout();
        }


        /// <summary>
        /// Occurs when the dialog is closed.
        /// </summary>
        public event DialogResultEventHandler Closed;
    }
}
